﻿using OpenCvSharp;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using static MvCamCtrl.NET.MyCamera;

namespace BaseTool
{

    /// <summary>
    /// 相机参数
    /// </summary>
    [Serializable]
    public class CameraParam
    {
        /// <summary> 相机采集超时时间 </summary>
        public int OverTime = 10000;
        /// <summary> 相机名字 </summary>
        public string Name = string.Empty;

        /// <summary> 相机参数 - 设置 </summary>
        public CameraHardwareParam Write = new CameraHardwareParam();
        /// <summary> 相机参数 - 读取 </summary>
        [XmlIgnore]
        public CameraHardwareParam Read { get; private set; } = new CameraHardwareParam();
        /// <summary> 相机参数 - 标定 </summary>
        public CameraHardwareParam Calibration = new CameraHardwareParam();
        /// <summary> 相机参数 - 重建 </summary>
        public CameraHardwareParam Reconstruct = new CameraHardwareParam();
    }


    /// <summary>
    /// 相机可设置参数
    /// </summary>
    [Serializable]
    public class CameraHardwareParam
    {
        /// <summary> 参数使能 </summary>
        public bool ParamEnable = false;

        //----图像格式
        /// <summary> 图像格式 </summary>
        public string PixelFormat
        {
            set
            {
                Enum.TryParse(value, out pixelFormat);
            }
            get
            {
                return pixelFormat.ToString();
            }
        }
        /// <summary> 图像格式 </summary>
        public MvGvspPixelType pixelFormat = MvGvspPixelType.PixelType_Gvsp_Mono8;
        /// <summary> 支持的图像格式 </summary>
        public string[] GetSupportPixelFormats
        {
            get
            {
                MvGvspPixelType[] types = new MvGvspPixelType[] {
                    MvGvspPixelType.PixelType_Gvsp_Mono8,
                    MvGvspPixelType.PixelType_Gvsp_RGB8_Packed,
                    MvGvspPixelType.PixelType_Gvsp_BGR8_Packed,
                    MvGvspPixelType.PixelType_Gvsp_BayerBG8,
                    MvGvspPixelType.PixelType_Gvsp_BayerGB8,
                    MvGvspPixelType.PixelType_Gvsp_BayerGR8,
                    MvGvspPixelType.PixelType_Gvsp_BayerRG8,
                };
                string[] strings = new string[types.Length];
                for (int i = 0; i < strings.Length; i++)
                {
                    strings[i] = types[i].ToString();
                }
                return strings;
            }
        }

        /// <summary> 硬触发源 </summary>
        public string TriggerSource
        {
            set
            {
                Enum.TryParse($"MV_TRIGGER_SOURCE_{value}", out triggerSource);
            }
            get
            {
                return triggerSource.ToString().Split('_').Last();
            }
        }
        /// <summary> 硬触发源 </summary>
        public MV_CAM_TRIGGER_SOURCE triggerSource = MV_CAM_TRIGGER_SOURCE.MV_TRIGGER_SOURCE_LINE0;
        /// <summary> 支持的硬触发源 </summary>
        public string[] GetSupportTriggerSource
        {
            get
            {
                MV_CAM_TRIGGER_SOURCE[] types = new MV_CAM_TRIGGER_SOURCE[] {
                    MV_CAM_TRIGGER_SOURCE.MV_TRIGGER_SOURCE_LINE0,
                    MV_CAM_TRIGGER_SOURCE.MV_TRIGGER_SOURCE_LINE1,
                    MV_CAM_TRIGGER_SOURCE.MV_TRIGGER_SOURCE_LINE2,
                    MV_CAM_TRIGGER_SOURCE.MV_TRIGGER_SOURCE_LINE3,
                };
                string[] strings = new string[types.Length];
                for (int i = 0; i < strings.Length; i++)
                {
                    strings[i] = types[i].ToString().Split('_').Last();
                }
                return strings;
            }
        }

        //----曝光
        /// <summary> 自动曝光使能 </summary>
        public bool AutoExposureEnable = false;
        /// <summary> 自动曝光 </summary>
        public CameraParamAutoMode1 AutoExposure = CameraParamAutoMode1.关闭;
        /// <summary> 曝光 </summary>
        public float ExposureTime = 5000;

        //----增益
        /// <summary> 自动增益使能 </summary>
        public bool AutoGainEnable = false;
        /// <summary> 自动增益 </summary>
        public CameraParamAutoMode1 AutoGain = CameraParamAutoMode1.关闭;
        /// <summary> 增益 </summary>
        public float Gain = 0;
    }

    /// <summary>
    /// 相机采集参数类
    /// </summary>
    public class CameraCapture
    {
        /// <summary> 采集模式 </summary>
        public CameraCaptureMode CaptureMode = CameraCaptureMode.连续采集指针模式;

        //----连续采集指针模式----
        /// <summary> 图像控件指针 </summary>
        public IntPtr HandlePictureBox = IntPtr.Zero;

        //----异步模式----
        /// <summary> 图像数据 </summary>
        public ConcurrentQueue<Mat> ReceiveImage = new ConcurrentQueue<Mat>();
        /// <summary> 图像数据数量 </summary>
        public int ReceiveImageCount { get { return ReceiveImage.Count; } }
        /// <summary> 最大缓存图像数量 </summary>
        public int ReceiveImageMaxCount = 1;
    }

    #region 枚举值
    /// <summary> 相机设置 采集模式 </summary>
    public enum CameraCaptureMode
    {
        连续采集指针模式,
        软触发采集异步模式,
        硬触发采集异步模式,
    }
    /// <summary> 相机设置 自动模式1 </summary>
    public enum CameraParamAutoMode1
    {
        关闭 = 0, 一次 = 1, 连续 = 2
    }
    /// <summary> 相机设置 自动模式2 </summary>
    public enum CameraParamAutoMode2
    {
        关闭 = 0, 连续 = 1, 一次 = 2
    }
    #endregion
}
